import textract
import chardet
import os
file = input('请输入文件或文件路径：')
#判断输入是否是文件
#如果是文件
if os.path.isfile(file):
    text = textract.process(file)
    print(chardet.detect(text))
    content = text.decode(chardet.detect(text).get('encoding'))



    searchStr = input('请输入查询关键字：')
    #查找文档中，文字的位置
    positions = []
    start = 0
    while True:
        position = content.find(searchStr,start)
        if position == -1:
            break
        positions.append(position)
        start = position + len(searchStr)
    print(positions)

    #查找上下换行符的位置
    for position in positions:
        end = content.find('\n',position)
        begin = content.rfind('\n',0,position)
        if begin < 0:
            begin = 0
        paragraph = content[begin:end]
        print(paragraph)
else:
    pass
'''
查找
'''
def searchContent():
    pass